﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.LIB;
using YArchitech.Revit;
using YJKCopyModel.Forms.GeothermalPipe;
using YJKCopyModel.InteractiveOperation;

namespace YJKCopyModel.GeothermalPipeQuickModel
{
	internal class GeothermalReequestMaker : InteractiveOperationBase
	{
		public GeothermalReequestMaker(ExternalCommandData useExternalCommandData) : base(useExternalCommandData)
		{
			this.m_uiDoc = useExternalCommandData.Application.ActiveUIDocument;
		}

		public override bool IfBreak
		{
			get
			{
				throw new NotImplementedException();
			}
		}

		public override void CloseFrom()
		{
			throw new NotImplementedException();
		}

		public override List<InputRequest> GetAllInputRequest()
		{
			return this.m_lstRequestes;
		}

		public override void PrepareRequest()
		{
			Document document = this.m_uiDoc.Document;
			List<ViewType> list = new List<ViewType>();
			list.Add((ViewType)1);
			list.Add((ViewType)2);
			bool flag = false;
			bool flag2 = false;
			bool flag3 = !YJKRevitTools.ViewCheck(this.m_uiDoc, list);
			if (flag3)
			{
				this.m_bCancel = true;
			}
			else
			{
				GeothermalPipeSelectForm geothermalPipeSelectForm = new GeothermalPipeSelectForm();
				DialogResult dialogResult = DialogResult.None;
				while ((dialogResult = geothermalPipeSelectForm.ShowDialog(this.m_handleRevit)) == DialogResult.OK)
				{
					bool flag4 = geothermalPipeSelectForm.DlgCloseType == CloseGeothermalDlgType.SelectSupplyLine;
					if (flag4)
					{
						bool flag5 = this.SupplyTmpCategory != null;
						if (flag5)
						{
							this.ShowLayer(document, ref this.SupplyTmpCategory);
						}
						DwgEntitiesSelection dwgEntitiesSelection = new DwgEntitiesSelection(this.m_uiDoc);
						Transaction transaction = new Transaction(document);
						try
						{
							transaction.Start("选择地热供水线");
							string strPickGuidInfo = string.Empty;
							bool isSame = GeothermalFormData.IsSame;
							if (isSame)
							{
								strPickGuidInfo = "请选择链接dwg图中的地盘图层";
							}
							else
							{
								strPickGuidInfo = "请选择链接dwg图中的地盘供水图层";
							}
							dwgEntitiesSelection.TryPickSameLayerCurve(out this.lstCurveInfoSupply, true, SelecteEntType.eLine | SelecteEntType.eArc | SelecteEntType.ePolyLine, strPickGuidInfo);
							transaction.Commit();
						}
						catch
						{
							bool flag6 = transaction.HasStarted();
							if (flag6)
							{
								transaction.RollBack();
							}
						}
						bool flag7 = this.lstCurveInfoSupply.Count > 0;
						if (flag7)
						{
							this.SupplyTmpCategory = this.lstCurveInfoSupply[0].RevitLayer;
							GeothermalFormData.SupplyLayer = this.SupplyTmpCategory.Name;
							//geothermalPipeSelectForm.pbxSupply.BackgroundImage = Resources.QuickModelGreen;
							bool getRadSameState = geothermalPipeSelectForm.GetRadSameState;
							if (getRadSameState)
							{
								geothermalPipeSelectForm.SetBtnOkEnableControl = true;
							}
							else
							{
								bool flag8 = flag2;
								if (flag8)
								{
									geothermalPipeSelectForm.SetBtnOkEnableControl = true;
								}
							}
							flag = true;
						}
						geothermalPipeSelectForm.pbxSupply.BackgroundImageLayout = ImageLayout.Stretch;
						this.geothermalRequest.LstSupplyCurveInfos.Clear();
						List<GeothermalCurveIfo> list2 = this.ReturnCurves(this.lstCurveInfoSupply);
						foreach (GeothermalCurveIfo geothermalCurveIfo in list2)
						{
							geothermalCurveIfo.bIsSameLayer = true;
							this.geothermalRequest.LstSupplyCurveInfos.Add(geothermalCurveIfo);
						}
					}
					else
					{
						bool flag9 = geothermalPipeSelectForm.DlgCloseType == CloseGeothermalDlgType.SelectBackLine;
						if (flag9)
						{
							DwgEntitiesSelection dwgEntitiesSelection2 = new DwgEntitiesSelection(this.m_uiDoc);
							Transaction transaction2 = new Transaction(document);
							try
							{
								transaction2.Start("选择地热回水线");
								string strPickGuidInfo2 = "请选择链接dwg图中的地盘回水图层";
								dwgEntitiesSelection2.TryPickSameLayerCurve(out this.lstCurveInfoBack, true, SelecteEntType.eLine | SelecteEntType.eArc | SelecteEntType.ePolyLine, strPickGuidInfo2);
								transaction2.Commit();
							}
							catch
							{
								bool flag10 = transaction2.HasStarted();
								if (flag10)
								{
									transaction2.RollBack();
								}
							}
							bool flag11 = this.lstCurveInfoBack.Count > 0;
							if (flag11)
							{
								this.BackTmpCategory = this.lstCurveInfoBack[0].RevitLayer;
								GeothermalFormData.BackLayer = this.BackTmpCategory.Name;
								//geothermalPipeSelectForm.pbxBack.BackgroundImage = Resources.QuickModelGreen;
								bool flag12 = !geothermalPipeSelectForm.GetRadSameState;
								if (flag12)
								{
									bool flag13 = flag;
									if (flag13)
									{
										geothermalPipeSelectForm.SetBtnOkEnableControl = true;
									}
								}
								flag2 = true;
							}
							geothermalPipeSelectForm.pbxBack.BackgroundImageLayout = ImageLayout.Stretch;
							this.geothermalRequest.LstBackCurveInfos.Clear();
							List<GeothermalCurveIfo> list3 = this.ReturnCurves(this.lstCurveInfoBack);
							foreach (GeothermalCurveIfo geothermalCurveIfo2 in list3)
							{
								geothermalCurveIfo2.bIsSameLayer = false;
								this.geothermalRequest.LstBackCurveInfos.Add(geothermalCurveIfo2);
							}
						}
						else
						{
							bool flag14 = geothermalPipeSelectForm.DlgCloseType == CloseGeothermalDlgType.NextButton;
							if (!flag14)
							{
								bool flag15 = this.SupplyTmpCategory != null;
								if (flag15)
								{
									this.ShowLayer(document, ref this.SupplyTmpCategory);
								}
								bool flag16 = this.BackTmpCategory != null;
								if (flag16)
								{
									this.ShowLayer(document, ref this.BackTmpCategory);
								}
								break;
							}
							bool flag17 = this.SupplyTmpCategory != null;
							if (flag17)
							{
								this.ShowLayer(document, ref this.SupplyTmpCategory);
							}
							bool flag18 = this.BackTmpCategory != null;
							if (flag18)
							{
								this.ShowLayer(document, ref this.BackTmpCategory);
							}
							bool isSame2 = GeothermalFormData.IsSame;
							if (isSame2)
							{
								GeothermalPipeSameSetForm geothermalPipeSameSetForm = new GeothermalPipeSameSetForm(this.m_uiDoc);
								DialogResult dialogResult2 = geothermalPipeSameSetForm.ShowDialog(this.m_handleRevit);
								bool flag19 = dialogResult2 == DialogResult.OK;
								if (flag19)
								{
									List<CurveInfo> list4 = new List<CurveInfo>();
									bool flag20 = geothermalPipeSameSetForm.DlgCloseType == CloseGeothermalDlgType.ByArea;
									if (flag20)
									{
										DwgEntitiesSelection dwgEntitiesSelection3 = new DwgEntitiesSelection(this.m_uiDoc);
										dwgEntitiesSelection3.TryGetEntitesInPickBox<CurveInfo>(out list4, this.lstCurveInfoSupply, new DelegateInRange<CurveInfo>(this.CheckCurveIsInRange), "请框选需转换的范围！");
										list4 = list4.FindAll((CurveInfo item) => item.UseLayerName == GeothermalFormData.SupplyLayer);
										this.geothermalRequest.LstSupplyCurveInfos.Clear();
										List<GeothermalCurveIfo> list5 = this.ReturnCurves(list4);
										foreach (GeothermalCurveIfo geothermalCurveIfo3 in list5)
										{
											geothermalCurveIfo3.bIsSameLayer = true;
											this.geothermalRequest.LstSupplyCurveInfos.Add(geothermalCurveIfo3);
										}
									}
									break;
								}
								this.m_bCancel = true;
								return;
							}
							else
							{
								GeothermalPipeDifSetForm geothermalPipeDifSetForm = new GeothermalPipeDifSetForm(this.m_uiDoc);
								bool flag21 = geothermalPipeDifSetForm.ShowDialog(this.m_handleRevit) == DialogResult.OK;
								if (flag21)
								{
									List<CurveInfo> list6 = new List<CurveInfo>();
									list6.AddRange(this.lstCurveInfoSupply);
									list6.AddRange(this.lstCurveInfoBack);
									List<CurveInfo> list7 = new List<CurveInfo>();
									bool flag22 = geothermalPipeDifSetForm.DlgCloseType == CloseGeothermalDlgType.ByArea;
									if (flag22)
									{
										DwgEntitiesSelection dwgEntitiesSelection4 = new DwgEntitiesSelection(this.m_uiDoc);
										dwgEntitiesSelection4.TryGetEntitesInPickBox<CurveInfo>(out list7, list6, new DelegateInRange<CurveInfo>(this.CheckCurveIsInRange), "请框选区域！");
										List<CurveInfo> lstCurveInfo = list7.FindAll((CurveInfo item) => item.UseLayerName == GeothermalFormData.SupplyLayer);
										List<CurveInfo> lstCurveInfo2 = list7.FindAll((CurveInfo item) => item.UseLayerName == GeothermalFormData.BackLayer);
										this.geothermalRequest.LstSupplyCurveInfos.Clear();
										List<GeothermalCurveIfo> list8 = this.ReturnCurves(lstCurveInfo);
										foreach (GeothermalCurveIfo geothermalCurveIfo4 in list8)
										{
											geothermalCurveIfo4.bIsSameLayer = true;
											this.geothermalRequest.LstSupplyCurveInfos.Add(geothermalCurveIfo4);
										}
										this.geothermalRequest.LstBackCurveInfos.Clear();
										list8 = this.ReturnCurves(lstCurveInfo2);
										foreach (GeothermalCurveIfo geothermalCurveIfo5 in list8)
										{
											geothermalCurveIfo5.bIsSameLayer = false;
											this.geothermalRequest.LstBackCurveInfos.Add(geothermalCurveIfo5);
										}
									}
									break;
								}
								this.m_bCancel = true;
								return;
							}
						}
					}
				}
				bool flag23 = dialogResult != DialogResult.OK;
				if (flag23)
				{
					this.m_bCancel = true;
				}
				else
				{
					this.m_lstRequestes.Add(this.geothermalRequest);
				}
			}
		}

		public bool CheckCurveIsInRange(CurveInfo curveInfo, PickedBox pickBox)
		{
			bool flag = curveInfo == null || pickBox == null;
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				XYZ endPoint = curveInfo.ThisCurve.GetEndPoint(0);
				XYZ endPoint2 = curveInfo.ThisCurve.GetEndPoint(1);
				XYZ xyz = new XYZ(Math.Min(pickBox.Min.X, pickBox.Max.X), Math.Min(pickBox.Min.Y, pickBox.Max.Y), 0.0);
				XYZ xyz2 = new XYZ(Math.Max(pickBox.Min.X, pickBox.Max.X), Math.Max(pickBox.Min.Y, pickBox.Max.Y), 0.0);
				bool flag2 = endPoint.X > xyz.X && endPoint.Y > xyz.Y && endPoint.X < xyz2.X && endPoint.Y < xyz2.Y;
				if (flag2)
				{
					result = true;
				}
				else
				{
					bool flag3 = endPoint2.X > xyz.X && endPoint2.Y > xyz.Y && endPoint2.X < xyz2.X && endPoint2.Y < xyz2.Y;
					result = flag3;
				}
			}
			return result;
		}

		private List<GeothermalCurveIfo> ReturnCurves(List<CurveInfo> lstCurveInfo)
		{
			List<GeothermalCurveIfo> list = new List<GeothermalCurveIfo>();
			foreach (CurveInfo curveInfo in lstCurveInfo)
			{
				GeothermalCurveIfo geothermalCurveIfo = new GeothermalCurveIfo();
				geothermalCurveIfo.cCurve = curveInfo.ThisCurve;
				XYZ endPoint = geothermalCurveIfo.cCurve.GetEndPoint(0);
				XYZ endPoint2 = geothermalCurveIfo.cCurve.GetEndPoint(1);
				bool flag = geothermalCurveIfo.cCurve is Line;
				if (flag)
				{
					try
					{
						geothermalCurveIfo.ptStart = new XYZ(endPoint.X, endPoint.Y, 0.0);
						geothermalCurveIfo.ptEnd = new XYZ(endPoint2.X, endPoint2.Y, 0.0);
						geothermalCurveIfo.cCurve = Line.CreateBound(geothermalCurveIfo.ptStart, geothermalCurveIfo.ptEnd);
						list.Add(geothermalCurveIfo);
					}
					catch (Exception ex)
					{
					}
				}
				else
				{
					bool flag2 = geothermalCurveIfo.cCurve is Arc;
					if (flag2)
					{
						try
						{
							XYZ xyz = geothermalCurveIfo.cCurve.Evaluate(0.5, true);
							geothermalCurveIfo.ptStart = new XYZ(endPoint.X, endPoint.Y, 0.0);
							geothermalCurveIfo.ptEnd = new XYZ(endPoint2.X, endPoint2.Y, 0.0);
							geothermalCurveIfo.cCurve = Arc.Create(geothermalCurveIfo.ptStart, geothermalCurveIfo.ptEnd, new XYZ(xyz.X, xyz.Y, 0.0));
							list.Add(geothermalCurveIfo);
						}
						catch (Exception ex2)
						{
						}
					}
				}
			}
			return list;
		}

		private void ShowLayer(Autodesk.Revit.DB.Document doc, ref Category layer)
		{
			Transaction transaction = new Transaction(doc);
			try
			{
				transaction.Start("恢复图层");
				doc.ActiveView.HYSetVisibility(layer, true);
				layer = null;
				transaction.Commit();
			}
			catch
			{
				bool flag = transaction.HasStarted();
				if (flag)
				{
					transaction.RollBack();
				}
			}
		}

		private List<CurveInfo> lstCurveInfoSupply = new List<CurveInfo>();

		private List<CurveInfo> lstCurveInfoBack = new List<CurveInfo>();

		private List<InputRequest> m_lstRequestes = new List<InputRequest>();

		private GeothermalRequest geothermalRequest = new GeothermalRequest();

		private UIDocument m_uiDoc;

		private Category SupplyTmpCategory = null;

		private Category BackTmpCategory = null;
	}
}
